//
//  LoopModel.m
//  K8彩票
//
//  Created by zhoubo on 2017/7/8.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "LoopModel.h"
#import <BmobSDK/Bmob.h>

@implementation LoopModel


+(instancetype)loopWithBmobObj:(BmobObject*)obj
{
    LoopModel* model = [[LoopModel alloc]    init];
    model.title = [obj objectForKey:@"title"];
    model.content = [obj objectForKey:@"content"];
    model.image = [obj objectForKey:@"image"];
    model.detailimgs = [obj objectForKey:@"detailimgs"];
    if (model.detailimgs==nil) {
        model.detailimgs = [NSArray arrayWithObject:model.image];
    }
    return model;
}

+(void)loadLoopsWithName:(NSString *)name success:(void (^)(NSArray *))success faild:(void (^)(NSError *))failed
{
    BmobQuery* query = [BmobQuery queryWithClassName:name];
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            failed(error);
        }
        else
        {
            NSMutableArray* arr = [[NSMutableArray alloc]initWithCapacity:array.count];
            
            for (BmobObject* obj in array) {
                LoopModel* loop  = [LoopModel loopWithBmobObj:obj] ;
                [arr addObject:loop];
            }
            
            success(arr.copy);
        }
        
        
        
    }];

}

@end
